package cn.tanghom.app.oa.service;

import java.util.Date;

import javax.annotation.Resource;

import lombok.val;

import org.duineframework.service.ClassificationService;
import org.flowable.engine.delegate.BpmnError;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import cn.tanghom.app.oa.controller.CandidateController;
import cn.tanghom.app.oa.model.Talent;
import cn.tanghom.app.oa.repository.TalentRepository;
import cn.tanghom.app.oa.service.impl.ResumeServiceWarper;

/**
 * 获取简历信息 
 * @author Hunteron-cp
 *
 */
@Service
public class TalentDataDelegate implements JavaDelegate {
  private ResumeServiceWarper resumeService = new ResumeServiceWarper();	
	
  @Autowired
  private TalentRepository talentRepository;   
  
  @Resource
  private CandidateController candidateController;
  
  @Resource
  private ClassificationService clsService;

  boolean useBean = !true;
  
  @Override
  public void execute(DelegateExecution execution) {
    Date now = new Date();
    Object talentId = execution.getVariable("talentId");
    if(talentId==null){
    	execution.setTransientVariable("errorMessage", "not existed!");
    	throw new BpmnError("BusinessExceptionOccurred");
    	//return ;
    }
    long italentId = (Long)Long.parseLong(talentId.toString());
    Talent talent = talentRepository.findOne(italentId);
    if(talent==null){
    	execution.setTransientVariable("errorMessage", "not existed!");    	
    	//throw new BpmnError("BusinessExceptionOccurred");
    	talent = new Talent(italentId,"");
    	//return ;
    }
    //execution.setTransientVariable("talentDto", talent);
    execution.setTransientVariable("talent", talent);
    if(useBean){
    	/**
	    val bean = candidateController.matchTalent(italentId);
	   
	    execution.setVariable("expectPositions", titleId);
	    if(build(talent,bean)){
	    	execution.setTransientVariable("talentBean", bean);
	    }
	    */
    }
    else{
    	execution.setVariable("expectPositions", talent.getExpect_position_titles());
    }
    
    String titleId = clsService.predictTitle("title", talent.getJobTitle());
    
    execution.setVariable("expectPositions2",talent.getExpect_position_titles());
    execution.setVariable("expectPositions", "技术");
    System.out.println("Faux call to backend for ["  + execution.getVariable("talentId") + "]"+talent.getUser_key());
  }

  /**
  public boolean build(Talent resume,McTalentBean bean){
	  if(bean.getAnnualSalaryMin()!=null){
		  resume.setAnnualSalary(bean.getAnnualSalaryMin().intValue());
	  }
	  if(bean.getAge()!=null){
		  resume.setBirthYear(2018-bean.getAge());
	  }
	  if(bean.getAge()!=null){
		  resume.setCityId(bean.getAge());
	  }
	  return true;
  }
  */
}
 